<?php
	namespace Api\Controller;
	use Think\Controller;
	header("Content-Type:text/html;charset=utf-8");
	class ScreenController extends Controller
	{
		/* 
		 *艺人库筛选 
		 *  
		 *  
		 */
// 		public function index()
// 		{
			//属性表读出才艺 属性（这些数据 是后台添加的）
// 			$TypeAttrData = M('UserTypeAttr')->where(array('attrname'=>'才艺'))->find();
// 			$CaiYi =  explode(',', $TypeAttrData['attrvalue']);
		
// 			$this->assign('CaiYi',$CaiYi);
// 			$this->display();
			
			
		
// 		}
		
		//异步返回json 艺人库筛选数据 ，需求厅 按照这个思路写就可以了
		public function artistScreen($longitude = null, $latitude = null)
		{
			
			if (I('areaname'))
			{
				if (I('areaname') == '(null)') 
				{
					$areaname = null;
				}
				else
				{
					$areaname = I('areaname');
				}
			}
			else
			{
				$areaname = null;
			}
				
			if (I('tagname'))
			{
				if (I('tagname') == '(null)') 
				{
					$tagname = null;
				}
				elseif (I('tagname') == '全部')
				{
					$tagname = null;
				}
				else
				{
					$tagname = I('tagname');
				}
			}
			else
			{
				$tagname = null;
			}
				
			if (I('resFilter'))
			{
				if (I('resFilter') == '(null)') 
				{
					$resFilter = null;
				}
				else
				{
					$resFilter = I('resFilter');
				}
			}
			else
			{
				$resFilter = null;
			}
				
			if (I('FilterAuth'))
			{
				$FilterAuth = I('FilterAuth');
			}
			elseif (I('FilterAuth') == '0')
			{
				$FilterAuth = I('FilterAuth');
			}
			else
			{
				$FilterAuth = "noLimit";
			}
			
			if (I('FilterUserType'))
			{
				$FilterUserType = I('FilterUserType');
			}
			elseif (I('FilterUserType') == '0')
			{
				$FilterUserType = I('FilterUserType');
			}
			else
			{
				$FilterUserType = "noLimit";
			}
			
			if (I('FilterPrice')) 
			{
				$FilterPrice = I('FilterPrice');
			}
			else
			{
				$FilterPrice = "noLimit";
			}
			
			if (I("resShort")) 
			{
				$reShort = I("resShort");
			}
			else
			{
				$reShort = null;
			}
			
// 			if(I('areaname') || I('tagname') || I('resFilter')){
			if($areaname != null || $tagname != null || $resFilter != null || $reShort != null || $FilterAuth != "noLimit" || $FilterUserType != "noLimit" || $FilterPrice != "noLimit")
			{
				//区域筛选ids
				if( !$AreaIds = $this->AreaFilter($areaname)){
					echo json_encode(array(
							'result'=>0,
							'message'=>'该区域暂无信息',
							'userlist'=>array()
					));
					exit;
				}
				else
				{
		
					//才艺筛选
					if( !$CaiyiIds = $this->CaiyiFilter($AreaIds)){
						echo json_encode(array(
								'result'=>0,
								'message'=>'才艺暂无信息',
								'userlist'=>array()
								//"caiyiIds"=>$CaiyiIds
						));
						exit;
					}
					else
					{
						//筛选
						if(!$filterIds = $this->SumFilter($CaiyiIds))
						{
							
							echo json_encode(array(
									'result'=>0,
									'message'=>'该筛选暂无信息',
									'userlist'=>array()
							));
							exit;
						}
						else
						{
// 							var_dump($AreaIds);
							$data = $this->sortRes($filterIds);
						}
					}
				}
				
		
		
				
		
		
		
				//排序
				//$data = $this->sortRes($filterIds);
				//返回数据
				echo json_encode($data);
			}
			else
			{
				$userInfos = M("User") -> field("uid,nickname,type,dtttype,rmtype") -> select();
				foreach ($userInfos as $k1=>$v1)
				{
					if ($v1["type"] == 0) 
					{
						if ($v1["dtttype"] ==1 && $v1["rmtype"]==1) 
						{
							$uid1[$k1] = $v1["uid"];
						}
						elseif ($v1["dtttype"] == 1 && $v1["rmtype"] == 0)
						{
							$uid2[$k1] = $v1["uid"];
						}
						elseif ($v1["dtttype"] == 0 && $v1["rmtype"] == 1)
						{
							$uid3[$k1] = $v1["uid"];
						}
						else
						{
							$uid4[$k1] = $v1["uid"];
						}
					}
					else
					{
						$uid5 = $v1["uid"];
					}
				}
				//已完善过资料和认证的用户
// 				var_dump($uid1);
// 				var_dump($uid2);
// 				var_dump($uid3);
// 				var_dump($uid4);
				if ($uid1 == null) 
				{
					$userInfos1 = null;
				}
				else
				{
					$where1["think_user.uid"] = array("in", $uid1);
					$userInfos1 = M("User") -> join("__USERINFO__ on __USER__.uid=__USERINFO__.uid") -> field("think_user.uid,nickname,dtttype,sex,tagname,headimg,spreadid,position") -> where($where1) -> select();
					foreach ($userInfos1 as $k2=>$v2)
					{
						if ($v2["tagname"] == null) 
						{
							$v2["tagname"] = array();
						}
						else
						{
							$tagname = explode("-", $v2["tagname"]);
							unset($v2["tagname"]);
							$v2["tagname"] = $tagname;
						}
						if ($v2["headimg"] == null) 
						{
							$v2["headimg"] = "";
						}
						if ($v2["spreadid"] == null) 
						{
							$v2["spread"] = "";
						}
						else
						{
							$condition1["spreadid"] = $v2["spreadid"];
							$spread = M("Spread") -> where($condition1) -> getField("spread");
							unset($v2["spreadid"]);
							$v2["spread"] = $spread;
						}
						if ($v2["position"] == null) 
						{
							$v2["position"] = "";
						}
						else
						{
							$position = explode("-", $v2["position"]);
							unset($v2["position"]);
							$v2["position"] = $position[1];
						}
						
						$userInfos1[$k2] = $v2;
					}
				}
				if ($uid2 == null) 
				{
					$userInfos2 = null;
				}
				else
				{
					$where2["uid"] = array("in", $uid2);
					$userInfos2 = M("User") -> field("uid,nickname,dtttype") -> where($where2) -> select();
					
					foreach ($userInfos2 as $k3=>$v3)
					{
						$v3["tagname"] = array();
						$v3["sex"] = "3";
						$v3["spread"] = "";
						$v3["headimg"] = "";
						$v3["position"] = "";
						$userInfos2[$k3] = $v3;
					}
				}
				if ($uid3 == null) 
				{
					$userInfos3 = null;
				}
				else
				{
					$where3["think_user.uid"] = array("in",$uid3); 
					$userInfos3 = M("User") -> join("__USERINFO__ on __USER__.uid=__USERINFO__.uid") -> field("think_user.uid,nickname,dtttype,sex,tagname,headimg,spreadid,position") -> where($where3) -> select();
					foreach ($userInfos3 as $k4=>$v4)
					{
						if ($v4["tagname"] == null)
						{
							$v4["tagname"] = array();
						}
						else
						{
							$tagname = explode("-", $v4["tagname"]);
							unset($v4["tagname"]);
							$v4["tagname"] = $tagname;
						}
						if ($v4["headimg"] == null)
						{
							$v4["headimg"] = "";
						}
						if ($v4["spreadid"] == null)
						{
							unset($v4["spreadid"]);
							$v4["spread"] = "";
						}
						else
						{
							$condition2["spreadid"] = $v4["spreadid"];
							$spread4 = M("Spread") -> where($condition2) -> getField("spread");
							unset($v4["spreadid"]);
							$v4["spread"] = $spread4;
						}
						if ($v4["position"] == null)
						{
							$v4["position"] = "";
						}
						else
						{
							$position = explode("-", $v4["position"]);
							unset($v4["position"]);
							$v4["position"] = $position[1];
						}
					
						$userInfos3[$k4] = $v4;
					}
				}
// 				var_dump($userInfos3);
				if ($uid4 == null) 
				{
					$userInfos4 = null;
				}
				else
				{
					$where4["uid"] = array("in",$uid4);
// 					$userInfos4 = M() -> field($field) -> where($where) -> select();
					$userInfos4 = M("User") -> field("uid,nickname,dtttype") -> where($where4) -> select();
						
					foreach ($userInfos4 as $k5=>$v5)
					{
						$v5["tagname"] = array();
						$v5["sex"] = "3";
						$v5["spread"] = "";
						$v5["headimg"] = "";
						$v5["position"] = "";
						$userInfos4[$k5] = $v5;
					}
				}
				
				if ($userInfos1 == null && $userInfos2 == null) 
				{
					$userInfos5 = null;
				}
				elseif ($userInfos1 == null)
				{
					$userInfos5 = $userInfos2;
				}
				elseif ($userInfos2 == null)
				{
					$userInfos5 = $userInfos1;
				}
				else
				{
					$userInfos5 = array_merge($userInfos1, $userInfos2);
				}
				
				if ($userInfos3 == null && $userInfos4 == null)
				{
					$userInfos6 = null;
				}
				elseif ($userInfos3 == null)
				{
					$userInfos6 = $userInfos4;
				}
				elseif ($userInfos4 == null)
				{
					$userInfos6 = $userInfos3;
				}
				else
				{
					$userInfos6 = array_merge($userInfos3, $userInfos4);
				}
				
				if ($userInfos5 == null && $userInfos6 == null)
				{
					$userInfos7 = null;
				}
				elseif ($userInfos5 == null)
				{
					$userInfos7 = $userInfos6;
				}
				elseif ($userInfos6 == null)
				{
					$userInfos7 = $userInfos5;
				}
				else
				{
					$userInfos7 = array_merge($userInfos5, $userInfos6);
				}
				
				if ($userInfos7 != null)
				{
					$array1['result'] = 0;
					$array1['message'] = "获取筛选成功";
					$array1['userlist'] = $userInfos7;
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
				else
				{
					$array1['result'] = 10902;
					$array1['message'] = "获取筛选失败";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
			}
		}
		
		/*
		 *需求厅筛选
		 *
		 *
		 */
		public function demandScreen(){
// 			$data1["FilterAuth"] = $_POST["FilterAuth"];
// 			$data1["FilterUserType"] = $_POST["FilterUserType"];
// 			$data["FilterPrice"] = $_POST["FilterPrice"];
			
// 			echo json_encode($data1);
			if (I('areaname')) 
			{
				if (I('areaname') == "(null)") 
				{
					$areaname = null;
				}
				else
				{
					$areaname = I('areaname');
				}
			}
			else
			{
				$areaname = null;
			}
			
			if (I('tagname'))
			{
				if (I('tagname') == "(null)") 
				{
					$tagname = null;
				}
				elseif (I('tagname') == '全部')
				{
					$tagname = null;
				}
				else
				{
					$tagname = I('tagname');
				}
			}
			else
			{
				$tagname = null;
			}
			
			if (I('resFilter'))
			{
				if (I('resFilter') == "(null)") 
				{
					$resFilter = null;
				}
				else
				{
					$resFilter = I('resFilter');
				}
			}
			else
			{
				$resFilter = null;
			}
			
			if (I('FilterAuth'))
			{
				$FilterAuth = I('FilterAuth');
			}
			elseif (I('FilterAuth') == '0')
			{
				$FilterAuth = I('FilterAuth');
			}
			else
			{
				$FilterAuth = "noLimit";
			}
			
			if (I('FilterUserType'))
			{
				$FilterUserType = I('FilterUserType');
			}
			elseif (I('FilterUserType') == '0')
			{
				$FilterUserType = I('FilterUserType');
			}
			else
			{
				$FilterUserType = "noLimit";
			}
			
			if (I('FilterPrice')) 
			{
				$FilterPrice = I('FilterPrice');
			}
			else
			{
				$FilterPrice = "noLimit";
			}
			
			if (I("reShort"))
			{
				$reShort = I("reShort");
			}
			else
			{
				$reShort = null;
			}
			
// 			if(I('areaname') || I('tagname') || I('resFilter') || I('FilterAuth')){
			if($areaname != null || $tagname != null || $resFilter != null || $reShort != null || $FilterAuth != "noLimit" || $FilterUserType != "noLimit" || $FilterPrice != "noLimit"){
				//区域筛选ids
// 				var_dump($areaname);
// 				var_dump($tagname);
				if( !$AreaIds = $this->demandAreaFilter($areaname)){
					//var_dump($areaname);
					echo json_encode(array(
							'result'=>0,
							'message'=>'该区域暂无信息',
// 							'areaname'=>$areaname,
							'userlist'=>array()
					));
					exit;
				}
				else
				{
					
					//才艺筛选
					if( !$CaiyiIds = $this->demandCaiyiFilter($AreaIds))
					{
						//var_dump($AreaIds);
						echo json_encode(array(
								'result'=>0,
								'message'=>'才艺暂无信息',
								'userlist'=>array()
								//"caiyiIds"=>$CaiyiIds
						));
						exit;
					}
					else
					{
						//筛选
						if(!$filterIds = $this->demandSumFilter($CaiyiIds))
						{
// 							var_dump($CaiyiIds);
							echo json_encode(array(
									'result'=>0,
									'message'=>'该筛选暂无信息',
									'userlist'=>array()
							));
							exit;
						}
						else
						{
							
							$data = $this->demandSortRes($filterIds);
// 							$data['get'] = I('get');
						}
					}
				}
		
		
		
		
		
				//排序
				//$data = $this->sortRes($filterIds);
				//返回数据
// 				var_dump(I('get'));
				echo json_encode($data);
			}
			else
			{
				$demandInfos = M("Demand") -> field("dmid,uid,aid,dmtitle,tagname,address,dtttype,dmtime,applystime") -> order("dtttype desc,dmtime desc") ->select();
				foreach ($demandInfos as $keydm => $valuedm)
				{
					//new小图标
					if ($valuedm["dmtime"] != null)
					{
						//发布距离现在的时间
						$Days = floor((time() - (strtotime($valuedm["dmtime"])))/86400);
						//var_dump($Days);
						if ($Days < 3)
						{
							//发布时距离现在的时间小于3天需要加new图标
							$icon = "1";
						}
						else
						{
							//发布时距离现在的时间大于3天不需要加new图标
							$icon = "0";
						}
						$valuedm["icon"] = $icon;
					}
					//地址
					if ($valuedm["address"] == null)
					{
						$valuedm["address"] = "";
					}
					else
					{
						$position = explode("-", $valuedm["address"]);
						unset($valuedm["address"]);
						$valuedm["address"] = $position[1];
					}
					//标签
					if ($valuedm["tagname"] == null)
					{
						$valuedm["tagname"] = array();
					}
					else
					{
						$tagname = explode("-", $valuedm['tagname']);
						unset($valuedm['tagname']);
						$valuedm["tagname"] = $tagname;
					}
					//剩余天数
					if ($valuedm["applystime"] != null)
					{
						$remainDay = floor((strtotime($valuedm["applystime"])-time())/86400);
						if ($remainDay < 0)
						{
							unset($valuedm["applystime"]);
							$valuedm["remainday"] = "-1";
						}
						else
						{
							unset($valuedm["applystime"]);
							$valuedm["remainday"] = $remainDay;
						}
					}
					else
					{
						unset($valuedm["applystime"]);
						$valuedm["remainday"] = "";
					}
					//发布需求时距离现在的时间
					$beforetime = $this->wordTime($valuedm["dmtime"]);
					unset($valuedm["dmtime"]);
					$valuedm["beforetime"] = $beforetime;
					
					if ($valuedm["uid"] == null)
					{
						//联系客服
						$valuedm["nickname"] = "";
						$valuedm["headimg"] = "";
						
						$demandInfos[$keydm] = $valuedm;
					}
					else
					{
						$condition1["uid"] = $valuedm["uid"];
						$demandUser = M("User") -> field("uid,nickname,type,dtttype,rmtype") -> where($condition1) -> find();
						if ($demandUser["type"] == 0)
						{
							if ($demandUser["rmtype"] == 0)
							{
								$demandUser["headimg"] = "";
							}
							else
							{
								$headimg = M("Userinfo") -> where($condition1) -> getField("headimg");
								if ($headimg == null)
								{
									$demandUser["headimg"] = "";
								}
								else
								{
									$demandUser["headimg"] = $headimg;
								}
							}
						}
						else
						{
							if ($demandUser["rmtype"] == 0)
							{
								$demandUser["headimg"] = "";
							}
							else
							{
								$headimg = M("Company") -> where($condition1) -> getField("cplogo");
								if ($headimg == null)
								{
									$demandUser["headimg"] = "";
								}
								else
								{
									$demandUser["headimg"] = $headimg;
								}
							}
						}
						$valuedm["nickname"] = $demandUser["nickname"];
						$valuedm["headimg"] = $demandUser["headimg"];
							
						$demandInfos[$keydm] = $valuedm;
					}
					
				}
				
				foreach ($demandInfos as $k=>$value)
				{
					$data1 = array(
							"dmid"=>$value["dmid"],
							"icon"=>$value["icon"],
							"dmtitle"=>$value["dmtitle"],
							"nickname"=>$value["nickname"],
							"dtttype"=>$value["dtttype"],
							"tagname"=>$value["tagname"],
							"remainday"=>$value["remainday"],
							"beforetime"=>$value["beforetime"],
							"headimg"=>$value["headimg"],
							"position"=>$value["address"]
					);
					$demandInfo1[$k] = $data1;
				}
				
				if ($demandInfo1 != null)
				{
					$array1['result'] = 0;
					$array1['message'] = "获取筛选成功";
					$array1['userlist'] = $demandInfo1;
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
				else
				{
					$array1['result'] = 10903;
					$array1['message'] = "获取筛选失败";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
			}
		}
		
		//先筛选区域(需要完善)
		public function AreaFilter($areaname)
		{
			if ($areaname == null) 
			{
				$areaFilterIds = M('User') -> getField('uid',true);
				return $areaFilterIds;
			}
			else
			{
				$condition["position"] = array("like", "%$areaname%");
				$areaFilterIds = M('Userinfo')->where($condition)->getField('uid',true);
				return $areaFilterIds;
			}
			
		}
		
		//需求厅先筛选区域(需要完善)
		public function demandAreaFilter($areaname)
		{
			if ($areaname == null) 
			{
				$areaFilterIds = M('Demand') -> getField('dmid',true);
				return $areaFilterIds;
			}
			else
			{
				$condition["address"] = array("like", "%$areaname%");
				$areaFilterIds = M('Demand')->where($condition)->getField('dmid',true);
				return $areaFilterIds;
			}
		}
		
		//才艺筛选
		public function CaiyiFilter($ids)
		{
			if (I('tagname'))
			{
				if (I('tagname') == "(null)") 
				{
					$tagname = null;
				}
				elseif (I('tagname') == '全部')
				{
					$tagname = null;
				}
				else
				{
					$tagname = I('tagname');
				}
			}
			else
			{
				$tagname = null;
			}
			//如果是选择才艺（即有参数）
			if($tagname != null)
			{
				$tagname = I('tagname');
				$tagnameArray1 = explode("-", $tagname);
				$tagnameInfo = M("Userinfo") -> field("uid,tagname") -> select();
// 				var_dump($tagnameInfo);
				foreach ($tagnameInfo as $key=>$value)
				{
					$tagnameArray2 = explode("-", $value["tagname"]);
					if (count(array_intersect($tagnameArray1, $tagnameArray2)) > 0) 
					{
						$Caiyiids[] = $value["uid"];
					}
				}
// 				$condition['uid']=array('in',$ids);
// 				$condition['tagname']=array('like',"%$tagname%");
// 				$Caiyiids = M('Userinfo')->where($condition)->getField('uid',true);
				//合并
// 				$Caiyiuids = array_merge($ids, $Caiyiids);
				//去重
// 				$Caiyiuuids = array_unique($Caiyiuids);
				//去重
				$Caiyiuids = array_intersect($ids, $Caiyiids);
				//var_dump($ids);
				return $Caiyiuids;
		
				//否则表示（全部）
			}
			else
			{
				return $ids;
			}
		
		}
		
		//需求厅才艺筛选
		public function demandCaiyiFilter($ids)
		{
			if (I('tagname'))
			{
				if (I('tagname') == "(null)")
				{
					$tagname = null;
				}
				elseif (I('tagname') == '全部')
				{
					$tagname = null;
				}
				else
				{
					$tagname = I('tagname');
				}
			}
			else
			{
				$tagname = null;
			}
			//如果是选择才艺（即有参数）
			if($tagname != null)
			{
				$tagname = I('tagname');
				
				$tagnameArray1 = explode("-", $tagname);
				$tagnameInfo = M("Demand") -> field("dmid,tagname") -> select();
// 				var_dump($tagnameInfo);
				foreach ($tagnameInfo as $key=>$value)
				{
					$tagnameArray2 = explode("-", $value["tagname"]);
					if (count(array_intersect($tagnameArray1, $tagnameArray2)) > 0)
					{
						$Caiyiids[] = $value["dmid"];
					}
				}
				
// 				$condition['dmid']=array('in',$ids);
// 				$condition['tagname']=array('like',"%$tagname%");
// 				$Caiyiids = M('Demand')->where($condition)->getField('dmid',true);
				
				//去重
				$Caiyidmids = array_intersect($ids, $Caiyiids);
				
// 				var_dump($Caiyidmids);
				return $Caiyidmids;
		
				//否则表示（全部）
			}
			else
			{
				return $ids;
			}
		
		}
		
		//综合筛选
		public function SumFilter($ids)
		{
			
// 			$FilterAuth = I("FilterAuth");
// 			$FilterUserType = I('FilterUserType');
// 			$FilterPrice = I('FilterPrice');

			if (I('FilterAuth'))
			{
				$FilterAuth = I('FilterAuth');
			}
			elseif (I('FilterAuth') == '0')
			{
				$FilterAuth = I('FilterAuth');
			}
			else
			{
				$FilterAuth = 'noLimit';
			}
			if (I('FilterUserType'))
			{
				$FilterUserType = I('FilterUserType');
			}
			elseif (I('FilterUserType') == '0')
			{
				$FilterUserType = I('FilterUserType');
			}
			else
			{
				$FilterUserType = 'noLimit';
			}
			if (I('FilterPrice'))
			{
				$FilterPrice = I('FilterPrice');
			}
			else
			{
				$FilterPrice = 'noLimit';
			}
			
// 			if ($FilterAuth == null || $FilterUserType==null || $FilterPrice == null)
// 			{
// 				$FilterAuth = 'noLimit';
// 				$FilterUserType = 'noLimit';
// 				$FilterPrice = 'noLimit';
// 			}
// 			else
// 			{
// 				$FilterAuth = I("FilterAuth");
// 				$FilterUserType = I('FilterUserType');
// 				$FilterPrice = I('FilterPrice');
// 			}
			
			if($FilterAuth == 'noLimit' && $FilterUserType == 'noLimit' && $FilterPrice == 'noLimit')
			{
				//var_dump($ids);
				return $ids;
		
			}
			else
			{
				//$condition['uid']=array('in',$ids);
				//价格这个表 最好能合并到其他表里
				//$condition["spreadid"]=$Filter['spread'];
				//$condition['dtttype']= $Filter['FilterAuth'];
				//$condition['type']= $Filter['FilterUserType'];
				//$condition['_logic'] = 'AND';
// 				$condition['uid']=array('in',$ids);
			if ($FilterAuth == 'noLimit')
				{
					if ($FilterUserType == 'noLimit')
					{
						$condition["spreadid"]=$FilterPrice;
						$userId = M("Userinfo")->where($condition)->getField('uid',true);
						
						$userIds = array_intersect($ids, $userId);
					}
					else
					{
						if ($FilterPrice == 'noLimit')
						{
							$condition['sex']= $FilterUserType;
							$userId = M("Userinfo")->where($condition)->getField('uid',true);
							
							$userIds = array_intersect($ids, $userId);
	// 						$condition['_logic'] = 'AND';
						}
						else
						{
							$condition1['sex']= $FilterUserType;
							$userId1 = M("Userinfo")->where($condition1)->getField('uid',true);
								
							$condition2["spreadid"]=$FilterPrice;
							$userId2 = M("Userinfo")->where($condition2)->getField('uid',true);
								
							
							if ($userId1 == null && $userId2 == null) 
							{
								$userIds = null;
							}
							elseif ($userId1 == null)
							{
								$userIds = $userId2;
							}
							elseif ($userId2 == null)
							{
								$userIds = $userId1;
							}
							else
							{
								$userId3 = array_intersect($userId1, $userId2);
							}
							
							if ($userId3 == null) 
							{
								$userIds = $ids;
							}
							else
							{
								$userIds = array_intersect($userId3, $ids);
							}
						}
					}
				}else{
					if($FilterUserType=='noLimit')
					{
						if($FilterPrice == 'noLimit')
						{
							$condition['dtttype']=$FilterAuth;
							$userId = M("User")->where($condition)->getField('uid',true);
								
							$userIds = array_intersect($ids, $userId);
						}
						else
						{
							$condition1['dtttype']=$FilterAuth;
							$userId1 = M("User")->where($condition1)->getField('uid',true);
								
							$condition2["spreadid"]=$FilterPrice;
							$userId2 = M("Userinfo")->where($condition2)->getField('uid',true);
							
							if ($userId1 == null && $userId2 == null)
							{
								$userIds = null;
							}
							elseif ($userId1 == null)
							{
								$userIds = $userId2;
							}
							elseif ($userId2 == null)
							{
								$userIds = $userId1;
							}
							else
							{
								$userId3 = array_intersect($userId1, $userId2);
							}
							
							if ($userId3 == null)
							{
								$userIds = $ids;
							}
							else
							{
								$userIds = array_intersect($userId3, $ids);
							}
						}
					}else{
						if($FilterPrice == 'noLimit')
						{
							$condition1['dtttype']=$FilterAuth;
							$condition2['sex']= $FilterUserType;
							$userId4 = M("User")->where($condition2)->getField('uid',true);
							
							$userIds = array_intersect($userId4, $ids);
						}
						else
						{
							$condition1['dtttype']=$FilterAuth;
							$condition2['sex']= $FilterUserType;
							$userId4 = M("User")->where($condition1)->getField('uid',true);
							
							$condition2["spreadid"]=$FilterPrice;
							$userId5 = M("Userinfo")->where($condition2)->getField('uid',true);
							
							if ($userId4 == null && $userId5 == null)
							{
								$userIds = null;
							}
							elseif ($userId4 == null)
							{
								$userIds = $userId5;
							}
							elseif ($userId5 == null)
							{
								$userIds = $userId4;
							}
							else
							{
								$userId6 = array_intersect($userId4, $userId5);
							}
								
							if ($userId6 == null)
							{
								$userIds = $ids;
							}
							else
							{
								$userIds = array_intersect($userId6, $ids);
							}
						}
					}
				}
// 				if ($FilterAuth == 'noLimit')
// 				{
// 					if ($FilterUserType == 'noLimit')
// 					{
// 						$condition["spreadid"]=$FilterPrice;
// 					}
// 					else
// 					{
// 						$condition['type']= $FilterUserType;
// 						$condition["spreadid"]=$FilterPrice;
// // 						$condition['_logic'] = 'AND';
// 					}
// 				}
// 				elseif ($FilterUserType == 'noLimit')
// 				{
// 					if ($FilterPrice == 'noLimit')
// 					{
// 						$condition['dtttype']= $FilterAuth;
// 					}
// 					else
// 					{
// 						$condition['dtttype']= $FilterAuth;
// 						$condition["spreadid"]=$FilterPrice;
// // 						$condition['_logic'] = 'AND';
// 					}
// 				}
// 				elseif ($FilterPrice == 'noLimit')
// 				{
// 					$condition['dtttype']= $FilterAuth;
// 					$condition['type']= $FilterUserType;
// // 					$condition['_logic'] = 'AND';
// 				}
// 				else
// 				{
// 					$condition["spreadid"]=$FilterPrice;
// 					$condition['dtttype']= $FilterAuth;
// 					$condition['type']= $FilterUserType;
// // 					$condition['_logic'] = 'AND';
// 				}
				
				
// 				$userId = M("Userinfo")->where($condition)->getField('uid',true);
				
// 				$userIds = array_intersect($ids, $userId);
// 				var_dump($userIds);
				return $userIds;
			}
		
		
		}
		
		//需求厅综合筛选
		public function demandSumFilter($ids)
		{
				
			//$Filter = array();
				
// 			$FilterAuth = I("FilterAuth");
// 			$FilterUserType = I('FilterUserType');
// 			$FilterPrice = I('FilterPrice');
			
// 			$FilterAuth = isset(I("FilterAuth"))?I("FilterAuth"):'noLimit';
// 			$FilterUserType = isset(I('FilterUserType'))?I('FilterUserType'):'noLimit';
// 			$FilterPrice = isset(I('FilterPrice'))?I('FilterPrice'):'noLimit';
			
			if (I('FilterAuth'))
			{
				$FilterAuth = I('FilterAuth');
			}
			elseif (I('FilterAuth') == '0')
			{
				$FilterAuth = '0';
			}
			else
			{
				$FilterAuth = 'noLimit';
			}
			if (I('FilterUserType'))
			{
				$FilterUserType = I('FilterUserType');
			}
			elseif (I('FilterUserType') == '0')
			{
				$FilterUserType = '0';
			}
			else
			{
				$FilterUserType = 'noLimit';
			}
			if (I('FilterPrice'))
			{
				$FilterPrice = I('FilterPrice');
			}
			else
			{
				$FilterPrice = 'noLimit';
			}
			
// 			if ($FilterAuth == null && $FilterUserType==null && $FilterPrice == null) 
// 			{
// 				$FilterAuth = 'noLimit';
// 				$FilterUserType = 'noLimit';
// 				$FilterPrice = 'noLimit';
// 			}
// 			else
// 			{
// 				$FilterAuth = I("FilterAuth");
// 				$FilterUserType = I('FilterUserType');
// 				$FilterPrice = I('FilterPrice');
// 			}
		
			if($FilterAuth == 'noLimit' && $FilterUserType == 'noLimit' && $FilterPrice == 'noLimit')
			{
				//var_dump($ids);
				return $ids;
		
			}else{
				//$condition['uid']=array('in',$ids);
				//价格这个表 最好能合并到其他表里
				//$condition["spreadid"]=$Filter['spread'];
				//$condition['dtttype']= $Filter['FilterAuth'];
				//$condition['type']= $Filter['FilterUserType'];
				//$condition['_logic'] = 'AND';
// 				$condition['dmid']=array('in',$ids);
				if ($FilterAuth == 'noLimit')
				{
					if ($FilterUserType == 'noLimit')
					{
						$condition["spreadid"]=$FilterPrice;
					}
					elseif ($FilterPrice == 'noLimit')
					{
						$condition['type']= $FilterUserType;
						
// 						$condition['_logic'] = 'AND';
					}
					else
					{
						$condition['type']= $FilterUserType;
						$condition["spreadid"]=$FilterPrice;
					}
				}else{
					if($FilterUserType=='noLimit')
					{
						if ($FilterPrice == 'noLimit') 
						{
							$condition['dtttype']=$FilterAuth;
						}
						else
						{
							$condition["spreadid"]=$FilterPrice;
							$condition['dtttype']=$FilterAuth;
						}
					}
					elseif($FilterPrice == 'noLimit')
					{
						if ($FilterUserType == 'noLimit') 
						{
							$condition['dtttype']=$FilterAuth;
						}
						else
						{
							$condition["type"]=$FilterUserType;
							$condition['dtttype']=$FilterAuth;
						}
					}
					else
					{
						$condition['dtttype']=$FilterAuth;
						$condition['type']= $FilterUserType;
						$condition["spreadid"]=$FilterPrice;
					}
				}
				
// 				if ($FilterAuth == 'noLimit')
// 				{
// 					if ($FilterUserType == 'noLimit')
// 					{
// 						$condition["spreadid"]=$FilterPrice;
// 					}
// 					else
// 					{
// 						$condition['type']= $FilterUserType;
// 						$condition["spreadid"]=$FilterPrice;
// // 						$condition['_logic'] = 'AND';
// 					}
// 				}
// 				elseif ($FilterUserType == 'noLimit')
// 				{
// 					if ($FilterPrice == 'noLimit')
// 					{
// 						$condition['dtttype']= $FilterAuth;
// 					}
// 					else
// 					{
// 						$condition['dtttype']= $FilterAuth;
// 						$condition["spreadid"]=$FilterPrice;
// // 						$condition['_logic'] = 'AND';
// 					}
// 				}
// 				elseif ($FilterPrice == 'noLimit')
// 				{
// 					$condition['dtttype']= $FilterAuth;
// 					$condition['type']= $FilterUserType;
// // 					$condition['_logic'] = 'AND';
// 				}
// 				else
// 				{
// 					$condition["spreadid"]=$FilterPrice;
// 					$condition['dtttype']= $FilterAuth;
// 					$condition['type']= $FilterUserType;
// // 					$condition['_logic'] = 'AND';
// 				}
		
				$userId = M("Demand")->where($condition)->getField('dmid',true);
		
				$userIds = array_intersect($ids, $userId);
// 				var_dump($userIds);
				return $userIds;
			}
		
		
		}
		
		//艺人库排序
		public function sortRes($ids)
		{
			$sortType = I('resShort');
			//var_dump($sortType);
			$condition['uid']= array('in',$ids);
			//请自行调整参数
			switch($sortType){
				case 'hot':
					$sort= 'seetimes desc';
					break;
				case 'new':
					$sort= 'regtime desc';
					break;
				case 'near':
					$longitude = I("longitude");
					$latitude = I("latitude");
// 					var_dump($longitude);
					if ($longitude == null || $latitude == null) 
					{
						
						$uid3 = null;
					}
					else
					{
						$dataInfo = M("User") -> field("uid,longitude,latitude") -> where($condition) ->select();
						foreach ($dataInfo as $key=>$value)
						{
							if ($value["longitude"] == null || $value["latitude"] == null) 
							{
								$uid2[] = $value['uid'];
							}
							else
							{
								$longitude1 = $value["longitude"];
								$latitude1 = $value["latitude"];
								$calculatedDistance = $this->getDistance($latitude, $longitude, $latitude1, $longitude1);
								$dataInfo[$key]["distance"] = $calculatedDistance;
							}
						}
						$flag = array();
						foreach ($dataInfo as $v)
						{
							$flag[] = $v["distance"];
						}
						$distanceInfo = array_multisort($flag, SORT_DESC, $dataInfo);
						foreach ($distanceInfo as $key1=>$value1)
						{
							$uid1[] = $value1["uid"];
						}
					}
					if ($uid1 == null && $uid2 == null) 
					{
						$uid3 = null;
					}
					elseif ($uid1 == null)
					{
						$uid3 = $uid2;
					}
					elseif ($uid2 == null)
					{
						$uid3 = $uid1;
					}
					else
					{
						$uid3 = array_merge($uid1, $uid2);
					}
					
					$condition1= array("in", $uid3);
					break;
				default:
					$sort= 'dtttype desc,rmtype desc,uid desc';
					break;
			}
		
			
			//这里自行关联其他表数据，我就不写了
			if (I('resShort') == 'near') 
			{
				if ($condition1 == null) 
				{
					$userInfo = array();
				}
				else
				{
					$userInfo = M("User") -> field("uid,nickname,rmtype,dtttype,type") -> where($condition1) ->select();
				}
			}
			else
			{
				$userInfo = M('User')-> field("uid,nickname,rmtype,dtttype,type") -> where($condition) -> order($sort) -> select();
			}
			//var_dump($userInfo);
			foreach ($userInfo as $key=>$value)
			{
				if ($value["type"] == 0) 
				{
					//个人
					if ($value["rmtype"] == 1) 
					{
						$where["uid"] = $value["uid"];
						
						$info = M("Userinfo") -> field("uid,sex,tagname,spreadid,headimg,position") -> where($where) -> find();
						if ($info["sex"] == null) 
						{
							$value["sex"] = "3";
						}
						else
						{
							$value["sex"] = $info["sex"];
						}
						if ($info["tagname"] == null) 
						{
							$value["tagname"] = array();
						}
						else
						{
							$tagname = explode("-", $info["tagname"]);
							unset($info["tagname"]);
							$value["tagname"] = $tagname;
						}
						if ($info["spreadid"] == null) 
						{
							unset($info["spreadid"]);
							$value["spread"] = "";
						}
						else
						{
							$where1["spreadid"] = $info["spreadid"];
							$spread = M("Spread") -> where($where1) -> getField("spread");
							unset($info["spreadid"]);
							$value["spread"] = $spread;
						}
						if ($info["headimg"] == null) 
						{
							$value["headimg"] = "";
						}
						else
						{
							$value["headimg"] = $info["headimg"];
						}
						if ($info["position"] == null) 
						{
							$value["position"] = "";
						}
						else
						{
							$position = explode("-", $info["position"]);
							unset($info["position"]);
							
							$value["position"] = $position[1];
						}
					}
					else
					{
						$value["tagname"] = array();
						$value["sex"] = "3";
						$value["spreadid"] = "";
						$value["headimg"] = "";
						$value["position"] = "";
					}
				}
				$userInfo[$key] = $value;
			}
			foreach ($userInfo as $userk=>$userValue)
			{
				if ($userValue["sex"] == null) 
				{
					$userValue["sex"] = "3";
				}
				if ($userValue["tagname"] == null)
				{
					$userValue["tagname"] = array();
				}
				if ($userValue["spread"] == null)
				{
					$userValue["spread"] = "";
				}
				if ($userValue["position"] == null)
				{
					$userValue["position"] = "";
				}
				$data1 = array(
						"uid"=>$userValue["uid"],
						"dtttype"=>$userValue["dtttype"],
						"nickname"=>$userValue["nickname"],
						"sex"=>$userValue["sex"],
						"tagname"=>$userValue["tagname"],
						"spread"=>$userValue["spread"],
						"headimg"=>$userValue["headimg"],
						"position"=>$userValue["position"]
				);
				$userInfo1[$userk] = $data1;
			}
			if ($userInfo1 == null) 
			{
				$data["result"] = 0;
				$data["message"] = "获取筛选失败";
				$data["userlist"] = array();
			}
			else
			{
				$data["result"] = 0;
				$data["message"] = "获取筛选成功";
				$data["userlist"] = $userInfo1;
			//var_dump($userInfo);
			}
			return $data;
		}
		
		//需求厅排序
		public function demandSortRes($ids)
		{
			$sortType = I('resShort');
			//var_dump($sortType);
			$condition['dmid']= array('in',$ids);
			//请自行调整参数
			switch($sortType){
				case 'hot':
					$sort= 'clicktimes desc';
					break;
				case 'new':
					$sort= 'dmtime desc';
					break;
				
				default:
					$sort= 'dtttype desc,dmtime desc,dmid desc';
					break;
			}
		
				
			//这里自行关联其他表数据，我就不写了
			
			$demandInfo = M('Demand')-> field("dmid,uid,dmtitle,address,dmtime,applystime,tagname,dtttype") -> where($condition) -> order($sort) -> select();
		
			//var_dump($userInfo);
			foreach ($demandInfo as $key=>$value)
			{
				//new小图标
				if ($value["dmtime"] != null)
				{
					//发布距离现在的时间
					$Days = floor((time() - (strtotime($value["dmtime"])))/86400);
					//var_dump($Days);
					if ($Days < 3)
					{
						//发布时距离现在的时间小于3天需要加new图标
						$icon = "1";
					}
					else
					{
						//发布时距离现在的时间大于3天不需要加new图标
						$icon = "0";
					}
					$value["icon"] = $icon;
				}
				//地址
				if ($value["address"] == null) 
				{
					$value["address"] = "";
				}
				else
				{
					$position = explode("-", $value["address"]);
					unset($value["address"]);
					$value["address"] = $position[1];
				}
				//标签
				if ($value["tagname"] == null) 
				{
					$value["tagname"] = array();
				}
				else
				{
					$tagname = explode("-", $value['tagname']);
					unset($value['tagname']);
					$value["tagname"] = $tagname;
				}
				//剩余天数
				if ($value["applystime"] != null)
				{
					$remainDay = floor((strtotime($value["applystime"])-time())/86400);
					if ($remainDay < 0)
					{
						unset($value["applystime"]);
						$value["remainday"] = "-1";
					}
					else
					{
						unset($value["applystime"]);
						$value["remainday"] = $remainDay;
					}
				}
				else
				{
					unset($value["applystime"]);
					$value["remainday"] = "";
				}
				//发布需求时距离现在的时间
				$beforetime = $this->wordTime($value["dmtime"]);
				unset($value["dmtime"]);
				$value["beforetime"] = $beforetime;
				
				if ($value["uid"] == null) 
				{
					//联系客服
					$value["nickname"] = "";
					$value["headimg"] = "";
						
					$demandInfo[$key] = $value;
				}
				else
				{
					$condition1["uid"] = $value["uid"];
					$demandUser = M("User") -> field("nickname,type,dtttype,rmtype") -> where($condition1) -> find();
					if ($demandUser["rmtype"] == 0) 
					{
						if ($demandUser["rmtype"] == 0) 
						{
							$demandUser["headimg"] = "";
						}
						else
						{
							$headimg = M("Userinfo") -> where($condition1) -> getField("headimg");
							if ($headimg == null) 
							{
								$demandUser["headimg"] = "";
							}
							else
							{
								$demandUser["headimg"] = $headimg;
							}
						}
					}
					else
					{
						if ($demandUser["rmtype"] == 0)
						{
							$demandUser["headimg"] = "";
						}
						else
						{
							$headimg = M("Company") -> where($condition1) -> getField("cplogo");
							if ($headimg == null)
							{
								$demandUser["headimg"] = "";
							}
							else
							{
								$demandUser["headimg"] = $headimg;
							}
						}
					}
					$value["nickname"] = $demandUser["nickname"];
					$value["headimg"] = $demandUser["headimg"];
					
					$demandInfo[$key] = $value;
				}
				
			}
			foreach ($demandInfo as $k=>$devalue)
			{
				$data1 = array(
						"dmid"=>$devalue["dmid"],
						"icon"=>$devalue["icon"],
						"dmtitle"=>$devalue["dmtitle"],
						"dtttype"=>$devalue["dtttype"],
						"nickname"=>$devalue["nickname"],
						"tagname"=>$devalue["tagname"],
						"remainday"=>$devalue["remainday"],
						"beforetime"=>$devalue["beforetime"],
						"headimg"=>$devalue["headimg"],
						"position"=>$devalue["address"]
				);
				$userInfo1[$k] = $data1;
			}
			if ($userInfo1 == null) 
			{
				$data["result"] = 0;
				$data["message"] = "获取筛选失败";
				$data["userlist"] = array();
			}
			else
			{
				$data["result"] = 0;
				$data["message"] = "获取筛选成功";
				$data["userlist"] = $userInfo1;
			}
			//var_dump($userInfo);
				
			return $data;
		}
		
		
		/**
		 * @desc 根据两点间的经纬度计算距离
		 * @param float $lat 纬度值
		 * @param float $lng 经度值
		 */
		protected function getDistance($lat1, $lng1, $lat2, $lng2)
		{
			$earthRadius = 6367000; //approximate radius of earth in meters
		
			/*
			 Convert these degrees to radians
			to work with the formula
			*/
		
			$lat1 = ($lat1 * pi() ) / 180;
			$lng1 = ($lng1 * pi() ) / 180;
		
			$lat2 = ($lat2 * pi() ) / 180;
			$lng2 = ($lng2 * pi() ) / 180;
		
			/*
			 Using the
			Haversine formula
		
			http://en.wikipedia.org/wiki/Haversine_formula
		
			calculate the distance
			*/
		
			$calcLongitude = $lng2 - $lng1;
			$calcLatitude = $lat2 - $lat1;
			$stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
			$stepTwo = 2 * asin(min(1, sqrt($stepOne)));
			$calculatedDistance = $earthRadius * $stepTwo;
		
			return round($calculatedDistance);
		}
		
		//求发布需求时距离现在的时间的方法
		protected function wordTime($time)
		{
			$times = strtotime($time);
			$int = time() - $times;
			$str = '';
			if ($int <= 2){
				$str = sprintf('刚刚', $int);
			}elseif ($int < 60){
				$str = sprintf('%d秒前', $int);
			}elseif ($int < 3600){
				$str = sprintf('%d分钟前', floor($int / 60));
			}elseif ($int < 86400){
				$str = sprintf('%d小时前', floor($int / 3600));
			}elseif ($int < 2592000){
				$str = sprintf('%d天前', floor($int / 86400));
			}else{
				$str = date('Y-m-d H:i:s', $time);
			}
			return $str;
		}
	}